package com.siao.loader
{
	
	/**
	 * 字节队列加载器
	 * @author SiaoLeon
	 */
	public class BytesQueueLoader extends BytesLoader
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		/**
		 * 队列长度
		 * @return
		 */
		public function get length():uint
		{
			return this.queue.length;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var queue:Vector.<BytesQueueLoaderInfo>;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function BytesQueueLoader()
		{
			this.queue = new Vector.<BytesQueueLoaderInfo>();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 将地址添加到队列并开始下载
		 * @param url 加载地址
		 * @param params 该地址所附参数
		 */
		override public function load(url:String=null, params:Object=null):void
		{
			var info:BytesQueueLoaderInfo = new BytesQueueLoaderInfo(url, params);
			this.queue.push(info);
			this.process();
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		override protected function save(content:*):void
		{
			super.save(content);
			this.process();
		}
		
		/**
		 * 处理队列
		 * @param event
		 */
		private function process():void
		{
			if (this.connected || this.queue.length < 1)
			{
				return;
			}
			
			if (this.queue.length < 1)
			{
				this.reset();
				return;
			}
			
			var info:BytesQueueLoaderInfo = this.queue.shift();
			super.load(info.url, info.params);
		}
	
	}
}

/**
 * 字节队列加载信息
 * @author SiaoLeon
 */
class BytesQueueLoaderInfo
{
	private var _url:String;
	
	/**
	 * 加载地址
	 * @return
	 */
	public function get url():String
	{
		return _url;
	}
	
	private var _params:Object;
	
	/**
	 * 附带参数
	 * @return
	 */
	public function get params():Object
	{
		return _params;
	}
	
	/**
	 * 构造函数
	 * @param url 加载地址
	 * @param params 附带参数
	 */
	function BytesQueueLoaderInfo(url:String, params:Object=null)
	{
		this._url = url;
		this._params = params;
	}

}
